package com.pen.interfece.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pen.interfece.Context.BaseContext;
import com.pen.interfece.mapper.DisciplinaryMapper;
import com.pen.interfece.mapper.StudentMapper;
import com.pen.interfece.pojo.PageResult;
import com.pen.interfece.pojo.dto.DisciplinaryPageQueryDTO;
import com.pen.interfece.pojo.dto.StudentDTO;
import com.pen.interfece.pojo.entity.Disciplinary;
import com.pen.interfece.pojo.entity.Dormitory;
import com.pen.interfece.pojo.entity.Student;
import com.pen.interfece.service.DisciplinarySerivice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DisciplinarySeriviceImpl implements DisciplinarySerivice {
    @Autowired
    private DisciplinaryMapper disciplinaryMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult pageQuery(DisciplinaryPageQueryDTO disciplinaryPageQueryDTO) {
        PageHelper.startPage(disciplinaryPageQueryDTO.getPage(),disciplinaryPageQueryDTO.getPageSize());
        Long id= BaseContext.getCurrentId();
        Page<Disciplinary> page=disciplinaryMapper.pageQuery(disciplinaryPageQueryDTO);
        long total = page.getTotal();
        List<Disciplinary> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void save(Disciplinary disciplinary) {
        disciplinaryMapper.save(disciplinary);
    }

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Disciplinary disciplinary=disciplinaryMapper.getById(id);
            //加回来分数
            int point=disciplinary.getNumber();
            int StuId=disciplinary.getStudentId();

            Student student=studentMapper.getStudentById(StuId);
            int point2=student.getPoint()+point;
            StudentDTO studentDTO=new StudentDTO();
            studentDTO.setId(StuId);
            studentDTO.setPoint(point2);
            studentMapper.update(studentDTO);
            disciplinaryMapper.deleteById(id);
        }
    }

    @Override
    public void update(Disciplinary disciplinary) {
        disciplinaryMapper.update(disciplinary);
    }
}
